import { FC, Key, useState } from 'react';
import { CreateModalList } from '@/biz-components/hoc-form/with-list';
import {
  ListProps,
  PageMetaConfig,
} from '../interface';
import { apiUrl, accountDelete, accountDisabled, accountEnable, accountRestPassword } from '../api';

import { message } from 'antd';
import { compileFormatMessage } from '@/utils/i18n-transform';
import { history } from "umi";
import { MicroAppWithMemoHistory } from 'umi';

const WithListComp = CreateModalList({});



const AccountList: FC<ListProps> = (props) => {
  const { match } = props;
  const [selectedTableRows, setSelectedTableRows] = useState<any[]>([]);

  /**
   * 批量启用
   */

  const handleEnable = (cb: any) => {
    if (!selectedTableRows.length) return;

    const ids = selectedTableRows
      .filter((item) => item.status === 'DISABLE')
      .map((item) => item.id);

    const params = {
      ids,
    };

    if (ids.length !== selectedTableRows.length) {
      message.warning(
        compileFormatMessage('account.enable.msg.warn', {
          count: selectedTableRows.length - ids.length,
        }),
      );
      return;
    }

    return accountEnable(params).then(() => {
      message.success(
        compileFormatMessage('account.enable.msg.success', {
          count: ids.length,
        }),
      );

      cb?.callback();
      setSelectedTableRows([]);
    });
  };

  /**
   * 批量禁用
   */
  const handleDisable = (cb: any) => {
    if (!selectedTableRows.length) return;
    const ids = selectedTableRows.filter((item) => item.status === 'ENABLE').map((item) => item.id);

    const params = {
      ids,
    };

    if (ids.length !== selectedTableRows.length) {
      message.warning(
        compileFormatMessage('account.disable.msg.warn', {
          count: selectedTableRows.length - ids.length,
        }),
      );
      return;
    }

    return accountDisabled(params).then(() => {
      message.success(
        compileFormatMessage('account.disable.msg.success', {
          count: ids.length,
        }),
      );
      cb?.callback();
      setSelectedTableRows([]);
    });
  };

  return (
      <>
        <MicroAppWithMemoHistory name="arcvAsm" url={`/workbench/doctype`} />;
    <WithListComp
      match={match}
      history={history}
      pageCode={PageMetaConfig.LIST}
      pageApi={apiUrl.list}
      rowSelection={{
        onChange: (rowKeys: Key[], selectedRows: any[]) => {
          setSelectedTableRows(selectedRows);
        },
      }}
      events={{
        buttonsEvents: {
          add: () => {
            history.push(PageMetaConfig.CREATE_LINK);
          },
          disable: handleDisable,
          enable: handleEnable,
        },
        tableButtonsEvents: {
          edit: (id: any, row: any, params: any) => {
            history.push(`${PageMetaConfig.EDIT_LINK}/${id}`);
          },
          detail: (id: any, row: any, params: any) => {
            history.push(`${PageMetaConfig.DETAIL_LINK}/${id}`);
          },
          handleDelete: (id: string) => {
            // accountDelete({ id }).then().finally();
          },
        },
      }}
    />
        </>
  );
};

export default AccountList;
